package com.ruoyi.web.controller.mobile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.mobile.bo.TAppUserAddBo;
import com.ruoyi.system.mobile.bo.TAppUserEditBo;
import com.ruoyi.system.mobile.bo.TAppUserQueryBo;
import com.ruoyi.system.mobile.domain.AppUser;
import com.ruoyi.system.mobile.service.AppUserService;
import com.ruoyi.system.mobile.vo.TAppUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * APP用户Controller
 * 
 * @author ruoyi
 * @date 2021-05-21
 */
@Api(value = "APP用户控制器", tags = {"APP用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mobile/appUser")
public class AppUserController extends BaseController {

    private final AppUserService iTAppUserService;

    /**
     * 查询APP用户列表
     */
    @ApiOperation("查询APP用户列表")
    @GetMapping("/list")
    public TableDataInfo<TAppUserVo> list(TAppUserQueryBo bo) {
        startPage();
        List<TAppUserVo> list = iTAppUserService.queryList(bo);
        return getDataTable(list);
    }



    /**
     * 获取APP用户详细信息
     */
    @ApiOperation("获取APP用户详细信息")
    @GetMapping("/{id}")
    public AjaxResult<TAppUserVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTAppUserService.queryById(id));
    }

    /**
     * 新增APP用户
     */
    @ApiOperation("新增APP用户")
    @Log(title = "APP用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult<Void> add(@RequestBody TAppUserAddBo bo) {
        return toAjax(iTAppUserService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改APP用户
     */
    @ApiOperation("修改APP用户")
    @Log(title = "APP用户", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult<Void> edit(@RequestBody TAppUserEditBo bo) {
        return toAjax(iTAppUserService.updateByEditBo(bo) ? 1 : 0);
    }


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        AppUser tAppUser = iTAppUserService.login(loginBody.getUsername(), loginBody.getPassword()
                );
        ajax.put("user", tAppUser);
        return ajax;
    }
}
